package com.jm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jm.common.exception.ServiceException;
import com.jm.common.util.Utils;
import com.jm.common.util.entity.Phone;
import com.jm.common.util.entity.SelectCondition;
import com.jm.dao.UserMapper;
import com.jm.pojo.AddressBook;
import com.jm.pojo.User;
import com.jm.pojo.dto.UserDto;
import com.jm.service.AddressBookService;
import com.jm.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private Utils utils;
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 发送验证码
     */
    @Override
    public boolean sendMessage(Phone phone) {
        String code = utils.setPhoneCodeCache(phone.getPhone());
        if (code == null || "".equals(code)) return false;
        return true;
    }

    /**
     * 验证验证码是否正确
     */
    @Override
    public boolean checkPhoneNumber(Phone phone) {
        String phoneCodeCache = utils.getPhoneCodeCache(phone.getPhone());
        return phone.getCode().equals(phoneCodeCache);
    }

    /**
     * 根据手机号判断是否注册
     */
    @Override
    public boolean checkUserIsExits(String phoneNumber) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(true, User::getPhone, phoneNumber);
        User user = getOne(qw);
        if (user == null) return false;
        return true;
    }

    /**
     * 根据手机号码获得用户对象
     */
    @Override
    public User getUserByPhone(String phoneNumber) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(true, User::getPhone, phoneNumber);
        User user = getOne(qw);
        return user;
    }

    @Override
    public IPage<User> selectByPageCondition(SelectCondition condition) {
        Long currentPage = condition.getPage();
        Long pageSize = condition.getPageSize();
//        判空
        if (currentPage == null || pageSize == null)
            throw new ServiceException("分页分页条件出现错误");
//        构造分页对象
        IPage<User> page = new Page<>(currentPage, pageSize);
//        构造查询条件
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.like(condition.getName() != null, User::getName, condition.getName());
//        进行查询
        page(page, qw);
        return page;
    }

    /**
     * 将单品对象转换为单品对象的扩展对象
     */
    @Override
    public UserDto toDto(User user) {
        UserDto dto = new UserDto();
        BeanUtils.copyProperties(user, dto);
        List<AddressBook> addressBooks = addressBookService.selectAddressByUserId(user.getId());
        dto.setAddressBooks(addressBooks);
        return dto;
    }

    /**
     * 将分页后的单品对象转换为分页后的扩展对象
     */
    @Override
    public IPage<UserDto> toDto(IPage<User> toyIPage) {
        List<UserDto> collect = toyIPage.getRecords().stream().map(new Function<User, UserDto>() {
            @Override
            public UserDto apply(User user) {
                return toDto(user);
            }
        }).collect(Collectors.toList());
        IPage<UserDto> dto = new Page<>();
        BeanUtils.copyProperties(toyIPage, dto, "records");
        dto.setRecords(collect);
        return dto;
    }

    @Override
    public boolean checkUsernameAndPassword(String phoneNumber, String password) {
        User userByData = getUserByPhone(phoneNumber);
        String dataBasePassword = userByData.getPassword();
        return dataBasePassword.equals(password);
    }

}
